//
//  DMAttentionDoctorViewController.m
//  MCNKuaiYi
//
//  Created by 袁情 on 17/1/8.
//  Copyright © 2017年 Mr.Cannon. All rights reserved.
//

#import "DMAttentionDoctorViewController.h"
#import "DMAttentionCell.h"
#import "DMDoctorModel.h"
#import "DMDetailViewController.h"

static NSString *attention = @"Attention";

@interface DMAttentionDoctorViewController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
//保存数据的数组
@property (nonatomic, strong) NSArray *attentionArr;
//上下拉刷新控件
@property (nonatomic, strong) MJRefreshNormalHeader *freshNormalHeader;
@property (nonatomic, strong) MJRefreshAutoFooter *freshAutoFooter;

@end

@implementation DMAttentionDoctorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    [self loadDoctordata];
}

#pragma mark - 数据源方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DMDoctorModel *model = _attentionArr[indexPath.row];
    DMAttentionCell *cell = [tableView dequeueReusableCellWithIdentifier:attention forIndexPath:indexPath];
    cell.model = model;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _attentionArr.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DMDetailViewController *detailView = [[DMDetailViewController alloc] init];
    
    detailView.view.backgroundColor = [UIColor whiteColor];
    
    detailView.model = self.attentionArr[indexPath.row];
    
    [self.navigationController pushViewController:detailView animated:YES];
}

//加载名医数据
- (void)loadDoctordata {
     BOOL isPullUp = YES;
    isPullUp = !_freshNormalHeader.isRefreshing;
    
    //获取当前的系统的时间戳
    NSInteger currentTime = [[NSDate date] timeIntervalSince1970];
    if (currentTime == 0) {
        currentTime = [[NSDate date] timeIntervalSince1970];
    }
    //定义
    NSString *URL = @"http://iosapi.itcast.cn/doctorList.json.php";;
    NSDictionary *parameters = @{@"user_id":@1000089,@"page_size":@15,@"page":@1};
    [DMDoctorModel getDoctorListDataWithURLString:URL andParameters:parameters andSuccessBlock:^(NSArray *newsList) {
        //赋值
        _attentionArr = newsList;
        //主线程刷新
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [_tableView reloadData];
            
            //如果是上拉
            if (isPullUp == YES) {
                [_freshAutoFooter endRefreshing];
            } else {
                [_freshNormalHeader endRefreshing];
                [DMTools dmShowSVSuccessTitle:@"刷新成功" andTime:1.0];
            }

        }];
    }];
}

#pragma mark - 初始化
- (void)setupUI {
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    //设置代理
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.rowHeight = 75;
    //去除多余部分
    _tableView.tableFooterView = [[UIView alloc] init];
    [_tableView registerClass:[DMAttentionCell class] forCellReuseIdentifier:attention];
    [self.view addSubview:_tableView];
    
//    /初始化上下拉刷新控件， 并且添加到tableView
    _freshNormalHeader = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadDoctordata)];
    
    _freshAutoFooter = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadDoctordata)];
    
    //将上下拉刷新的控件添加到tableView上
    _tableView.mj_header = _freshNormalHeader;
    _tableView.mj_footer = _freshAutoFooter;
    
    
}


@end
